package com.swjd.controller;

import com.swjd.service.MyAsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author liutao
 */
@RestController
public class MyAsyncController {
    @Autowired
    private MyAsyncService myAsyncService;

    @GetMapping("/sendSMS")
    public String sendSMS() throws Exception {
        long startTime = System.currentTimeMillis();
        myAsyncService.sendSMS();
        long endTime = System.currentTimeMillis();
        System.out.println("主流程序耗时：" + (endTime - startTime));
        return "success";
    }

    @GetMapping("/statistics")
    public String statistics() throws InterruptedException, ExecutionException {
        long startTime = System.currentTimeMillis();
        Future<Integer> processA = myAsyncService.processA();
        Future<Integer> processB = myAsyncService.processB();
        int total = processA.get() + processB.get();
        System.out.println("异步任务数据汇总结果：" + total);
        long endTime = System.currentTimeMillis();
        System.out.println("主流程序耗时：" + (endTime - startTime));
        return "success";
    }
}
